package com.lwlk.zdk.service.User.impl;

import com.lwlk.zdk.enums.RetCode;
import com.lwlk.zdk.mapper.sys.sysUserMapper;
import com.lwlk.zdk.model.sys.sysUser;
import com.lwlk.zdk.service.User.SysUserService;
import com.lwlk.zdk.util.MD5Utils;
import com.lwlk.zdk.util.Response;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.List;
import java.util.Map;

@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    sysUserMapper sysUserMapper;


    @Override
    public List<Map<String,Object>> searchAll() {
        List<Map<String,Object>>userList=sysUserMapper.searchAll();
        return userList;
    }

    @Override
    public List<Map<String,Object>> searchChoice(sysUser sysUser) {
        List<Map<String,Object>>userList=sysUserMapper.searchChoice(sysUser);
        return userList;
    }

    @Override
    public List<Map<String, Object>> selectAll(Integer platformIds) {
        List<Map<String,Object>>userAndVehicle=sysUserMapper.selectAll(platformIds);
        return userAndVehicle;
    }

    @Override
    public Integer selectPlatformId(String roleType) {
        Integer platformIds=sysUserMapper.selectPlatform(roleType);
        return null;
    }

    @Override
    public String selectPlatformIds(String superiorAreaCode) {
        String platformId=sysUserMapper.selectPlatformIds(superiorAreaCode);
        return platformId;
    }

    @Override
    public List<Map<String, Object>> selectPlatform(String areaCode) {
        List<Map<String,Object>>platformList=sysUserMapper.selectByAreaCode(areaCode);
        return platformList;
    }

    @Override
    public Response login(String account, String password) {
        Response response=new Response();
        try {

            String pwd= MD5Utils.encrypt(account,password);
            List<Map<String,Object>>platform=sysUserMapper.selectOne(account);
            if (platform==null){//账号未存在
                response.setCode(RetCode.ACCOUNT_NULL);
                return response;
            }
            if (!pwd.equals(platform.get(0).get("password"))){//密码错误
                response.setCode(RetCode.PWD_ERROR);
                return response;
            }
            UsernamePasswordToken token = new UsernamePasswordToken(account, password);
            Subject subject = SecurityUtils.getSubject();
            subject.login(token);



        }catch (Exception e){
            e.printStackTrace();
        }
        return response;
    }

    @Override
    public void saveFile(InputStream inputStream, String fileName) {

    }

    @Override
    public String selectBypId(Integer pId) {
        String platformName=sysUserMapper.selectBypId(pId);
        return platformName;
    }

    @Override
    public String selectDimPlatformName(String platformName) {
        String pIds=sysUserMapper.selectDimPlatformName(platformName);
        return pIds;
    }

    @Override
    public int selectAccount(String account) {
        int count=sysUserMapper.selectAccount(account);
        return count;
    }

    @Override
    public List<Map> selectPlatformIdsList(String pName) {
        List<Map>pIdsList=sysUserMapper.selectPlatformIdsList(pName);
        return pIdsList;
    }
}
